# with <method_name>() as d:
# with 后的结果对象，需要重写__enter__和__exit__方法
# 当进入with代码块时自动调用__enter__方法
# 当离开with代码块时自动调用__exit__方法


class Demo(object):
    def __enter__(self):
        print('__enter__ed')
        return self
        # return '__enter__的返回值'

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('__exit__ed')


def creat_demo():
    x = Demo()
    return x


# d = creat_demo().__enter__()
with Demo() as d:  # 过程的理解：with creat_demo() as d:
    print('d is ', d)
